package com.zzb.projectfactory.framework.role.controller;

import com.zzb.projectfactory.framework.common.BaseController;
import com.zzb.projectfactory.framework.common.BasePage;
import com.zzb.projectfactory.framework.common.BaseResponse;
import com.zzb.projectfactory.framework.common.ServiceException;
import com.zzb.projectfactory.framework.role.entity.SysRoleMenu;
import com.zzb.projectfactory.framework.role.entity.User;
import com.zzb.projectfactory.framework.role.form.SysRoleMenuSearchForm;
import com.zzb.projectfactory.framework.role.service.SysRoleMenuService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
* 菜单角色关系表controller
*/
@Controller
@RequestMapping("/system/sysRoleMenu")
public class SysRoleMenuController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SysRoleMenuController.class);
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**菜单角色关系表管理首页*/
    @RequestMapping({"","/","/index"})
    public String index(){
        return "system/sysRoleMenu/index";
    }
    /**菜单角色关系表列表json*/
    @RequestMapping("list")
    @ResponseBody
    public BasePage<SysRoleMenu> list(SysRoleMenuSearchForm sysRoleMenuSearchForm){
        return sysRoleMenuService.queryPage(sysRoleMenuSearchForm, SysRoleMenu.class);
    }

    /**菜单角色关系表表单页面*/
    @RequestMapping("form")
    public String form(String id, Model model){
        SysRoleMenu sysRoleMenu;
        if(StringUtils.isEmpty(id)){
            sysRoleMenu = new SysRoleMenu();
    } else{
        sysRoleMenu = sysRoleMenuService.get(id);
        if(sysRoleMenu == null) sysRoleMenu = new SysRoleMenu();
    }
        model.addAttribute("sysRoleMenu", sysRoleMenu);
        return "system/sysRoleMenu/form";
    }

    /**菜单角色关系表保存*/
    @RequestMapping("/save")
    @ResponseBody
    public BaseResponse<SysRoleMenu> save(SysRoleMenu sysRoleMenu, HttpServletRequest request){
        try {
            User user = getCurrentUser(request);
            return sysRoleMenuService.saveRoleMenu(sysRoleMenu, user);
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse<>(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**菜单角色关系表删除*/
    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse delete(String id,HttpServletRequest request){
        try {
            return sysRoleMenuService.deleteRoleMenu(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**菜单角色关系表开启*/
    @RequestMapping("/open")
    @ResponseBody
    public BaseResponse open(String id ,HttpServletRequest request){
        try {
            return sysRoleMenuService.recovery(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**菜单角色关系表屏蔽*/
    @RequestMapping("/deny")
    @ResponseBody
    public BaseResponse deny(String id ,HttpServletRequest request){
        try {
            return sysRoleMenuService.trash(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping("/deleteByRoleIdAndMenuId")
    public BaseResponse deleteByRoleIdAndMenuId(String roleId, String menuId){
        sysRoleMenuService.removeByRoleIdAndMenuId(roleId,menuId);
        return new BaseResponse(BaseResponse.SUCCESS,"删除菜单权限成功");
    }

    @ResponseBody
    @RequestMapping("/createByRoleIdAndMenuId")
    public BaseResponse createByRoleIdAndMenuId(String roleId,String menuId,HttpServletRequest request){
        User currentUser = getCurrentUser(request);
        return sysRoleMenuService.createByRoleIdAndMenuId(roleId,menuId,currentUser);
    }

}
